{
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
}

unit ScriptModifierEditor;

interface

uses
  Classes, SysUtils, FileUtil, SynEdit, Forms, Controls, ExtCtrls, StdCtrls,
  ModifierEditors, TestUiUtils, TestCore, SynHighlighterPas, Graphics,
  SynEditMarkupWordGroup, SynEditKeyCmds, LCLType, Script;

type
  TScriptModifierEditorFrame = class(TFrame, IModifierEditor)
    bvlStatus: TBevel;
    lblStatus: TLabel;
    pnlMain: TPanel;
  private
    FModifier: TScriptModifier;
    FSynEdit: TSynEdit;
    FPasHighlighter: TSynPasSyn;
    procedure CreatePasHighlighter;
    procedure CreateSynEdit;
    procedure SynEditKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure SynEditStatusChange(Sender: TObject; Changes: TSynStatusChanges);
    { private declarations }
  public
    destructor Destroy; override;
    procedure SetUp(Modifier: TModifier);
    procedure Commit;
    { public declarations }
  end;

implementation

{$R *.lfm}

{ TScriptModifierEditorFrame }

procedure TScriptModifierEditorFrame.CreatePasHighlighter;
var
  i: Integer;
begin
  FPasHighlighter := TSynPasSyn.Create(nil);
  FPasHighlighter.CommentAttri.Foreground := clSilver;
  FPasHighlighter.CommentAttri.Style := [];
  FPasHighlighter.DirectiveAttri.Foreground := clGreen;
  FPasHighlighter.DirectiveAttri.Style := [];
  FPasHighlighter.IdentifierAttri.Foreground := clLime;
  FPasHighlighter.KeyAttri.Foreground := clAqua;
  FPasHighlighter.NumberAttri.Foreground := clFuchsia;
  FPasHighlighter.StringAttri.Foreground := clYellow;
  FPasHighlighter.SymbolAttri.Foreground := clAqua;

  for i := 0 to FPasHighlighter.DividerDrawConfigCount-1 do
    FPasHighlighter.DividerDrawConfig[i].MaxDrawDepth := 0;
end;

procedure DisableKeystroke(SynEdit: TSynEdit; Command: TSynEditorCommand);
var
  i: Integer;
begin
  i := SynEdit.Keystrokes.FindCommand(Command);
  if i <> -1 then
    SynEdit.Keystrokes.Delete(i);
end;

procedure TScriptModifierEditorFrame.CreateSynEdit;
const
  DISABLE_KEYSTROKES: array [0..11] of TSynEditorCommand = (
    EcFoldLevel1, EcFoldLevel2, EcFoldLevel3, EcFoldLevel4, EcFoldLevel5,
    EcFoldLevel6, EcFoldLevel7, EcFoldLevel8, EcFoldLevel9, EcFoldLevel0,
    EcFoldCurrent, EcUnFoldCurrent);
var
  Command: TSynEditorCommand;
begin
  FSynEdit := TSynEdit.Create(nil);
  FSynEdit.SetDefaultKeystrokes;
  FSynEdit.Highlighter := FPasHighlighter;
  FSynEdit.Align := alClient;
  FSynEdit.BorderStyle := bsNone;
  FSynEdit.Parent := pnlMain;

  FSynEdit.Color := clBlack;
  FSynEdit.Font.Color := clWhite;
  FSynEdit.Gutter.Parts.Clear;
  FSynEdit.Options := FSynEdit.Options - [eoSmartTabs] + [eoHideRightMargin, eoTabIndent];
  FSynEdit.TabWidth := 2;
  FSynEdit.SelectedColor.Background := clWhite;
  FSynEdit.SelectedColor.Foreground := clBlack;
  FSynEdit.MarkupByClass[TSynEditMarkupWordGroup].MarkupInfo.FrameColor := clMedGray;

  for Command in DISABLE_KEYSTROKES do
    DisableKeystroke(FSynEdit, Command);

  FSynEdit.OnKeyDown := SynEditKeyDown;
  FSynEdit.OnStatusChange := SynEditStatusChange;
end;

procedure TScriptModifierEditorFrame.SynEditKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  if (Key = VK_ESCAPE) and (Shift = []) then
    Key := 0;
end;

procedure TScriptModifierEditorFrame.SynEditStatusChange(Sender: TObject;
  Changes: TSynStatusChanges);
begin
  lblStatus.Caption := Format(' %d: %d', [FSynEdit.CaretY, FSynEdit.CaretX]);
end;

destructor TScriptModifierEditorFrame.Destroy;
begin
  FreeAndNil(FSynEdit);
  FreeAndNil(FPasHighlighter);
  inherited;
end;

procedure TScriptModifierEditorFrame.SetUp(Modifier: TModifier);
begin
  FModifier := Modifier as TScriptModifier;
  CreatePasHighlighter;
  CreateSynEdit;

  FModifier.GetScript(FSynEdit.Lines);
end;

procedure TScriptModifierEditorFrame.Commit;
begin
  FModifier.SetScript(FSynEdit.Lines);
end;

initialization

  ModifierEditorRegistry.Add(TScriptModifierEditorFrame, TScriptModifier);

end.

